import { defineStore } from 'pinia'
import { ref, reactive } from 'vue'

export const useDataStore = defineStore('data', () => {
  // 确保存储结构正确初始化
  const statistics = reactive({
    bankData: 0,
    orderData: 0,
    announcementData: 0,
    total: 0
  })

  const syncStats = reactive({
    bank: 0,
    orders: 0,
    announcements: 0,
    total: 0
  })

  const loadStatistics = async () => {
    // 加载统计数据
    console.log('加载统计数据')
    // 确保统计数据结构正确
    if (typeof statistics.bankData !== 'number') statistics.bankData = 0
    if (typeof statistics.orderData !== 'number') statistics.orderData = 0
    if (typeof statistics.announcementData !== 'number') statistics.announcementData = 0
    if (typeof statistics.total !== 'number') statistics.total = 0
    
    // 确保同步统计数据结构正确
    if (typeof syncStats.bank !== 'number') syncStats.bank = 0
    if (typeof syncStats.orders !== 'number') syncStats.orders = 0
    if (typeof syncStats.announcements !== 'number') syncStats.announcements = 0
    if (typeof syncStats.total !== 'number') syncStats.total = 0
  }

  return {
    statistics,
    syncStats,
    loadStatistics
  }
})